//
//  BXPickerView.m
//  TenThousand
//
//  Created by 我的 on 16/7/7.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "BXPickerView.h"

@implementation BXPickerView

{
    NSMutableArray *_arr;
    NSString *_text;
    NSInteger _num;
    NSInteger _index;
}

- (id)initWithFrame:(CGRect)frame WithTitleListArr:(NSMutableArray *)listArr WithIndex:(NSInteger)index
{
    self = [super initWithFrame:frame];
    if (self) {
        // 选择框
        self.frame = CGRectMake(0, Screen_Height-250, Screen_Width, 250);
        self.backgroundColor = [UIColor whiteColor];
        // 显示选中框
        self.dataSource = self;
        self.delegate = self;
        _arr = listArr;
        _num = index;
    }
    return self;
}


//点击确定
-(void)sureClick{
    if ([_text isEqualToString:@""]) {
        _clickedBtnIndexBlock(_arr[0], 0);
    }else
    
    _clickedBtnIndexBlock(_text, _index);
    [_backView removeFromSuperview];
}
- (void)cancelClick{
    _clickedBtnIndexBlock(nil, nil);
    [_backView removeFromSuperview];
}

// pickerView 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return _num;
}

// pickerView 每列个数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [_arr count];
}

//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    _text = [_arr objectAtIndex:row];
    return [_arr objectAtIndex:row];
}

// 每列宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
  
    return Screen_Width/_num;
}


- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 40;
}

// 返回选中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _clickedBtnIndexBlock(_arr[row], row);
    _text = _arr[row];
    _index = row;

}


-(void)removeView
{
    _clickedBtnIndexBlock(nil, nil);
    [_backView removeFromSuperview];
}

-(void)showView
{
    
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];

    [UIView  animateWithDuration:0.2 animations:^{
        _backView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height)];
        _backView.backgroundColor = [UIColor colorWithRed:0.16 green:0.17 blue:0.21 alpha:0.5];
        [window addSubview:_backView];
        [_backView addSubview:self];
    }];

    
    UIToolbar *pickerDateToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, Screen_Height-220-40, Screen_Width, 44)];
    pickerDateToolbar.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.00];
    
    [pickerDateToolbar sizeToFit];
    
    
    
    NSMutableArray *barItems = [[NSMutableArray alloc] init];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];
    
    
    NSMutableArray *myToolBarItems = [NSMutableArray array];
    [myToolBarItems addObject:[[UIBarButtonItem alloc]
                               
                               initWithTitle:@"  "
                               
                               style:UIBarButtonItemStylePlain
                               
                               target:self
                               
                               action:@selector(action)]];
    [myToolBarItems addObject:[[UIBarButtonItem alloc]
                               
                               initWithTitle:@"取消"
                               
                               style:UIBarButtonItemStylePlain
                               
                               target:self
                               
                               action:@selector(cancelClick)]];
    [myToolBarItems addObject:[[UIBarButtonItem alloc]
                               
                               initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                               target:self
                               action:nil]];
    
    [myToolBarItems addObject:[[UIBarButtonItem alloc]
                               
                               initWithTitle:@"完成"
                               
                               style:UIBarButtonItemStylePlain
                               
                               target:self
                               
                               action:@selector(sureClick)]];
    [myToolBarItems addObject:[[UIBarButtonItem alloc]
                               
                               initWithTitle:@"  "
                               
                               style:UIBarButtonItemStylePlain
                               
                               target:self
                               
                               action:@selector(action)]];
    
    [pickerDateToolbar setItems:myToolBarItems animated:YES];
    [pickerDateToolbar items];
    
    [_backView addSubview:pickerDateToolbar];

    
    
    [_backView addTarget:self action:@selector(removeView) forControlEvents:UIControlEventTouchUpInside];

}


@end
